package itsm.isperp.module.controller.sparepart;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseProcessController;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.provider.PrdProvider;
import itsm.isperp.module.entity.sparepart.SptSparepartIn;
import itsm.isperp.module.service.app.AppUserService;
import itsm.isperp.module.service.project.ProjProjectService;
import itsm.isperp.module.service.provider.PrdProviderService;
import itsm.isperp.module.service.sparepart.SptSparepartInDetailService;
import itsm.isperp.module.service.sparepart.SptSparepartInService;
import itsm.isperp.module.service.sparepart.SptSparepartService;
import itsm.isperp.workflow.service.BpmProcessRunService;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 备件管理控制器
 * 
 * @author lizx
 * @date 2013-9-12
 */
@Controller
@RequestMapping("sparepart/sparepartIn")
public class SptSparepartInController extends
    BaseProcessController<SptSparepartIn> {

  @Autowired
  protected SptSparepartInService sptSparepartInService;

  @Autowired
  protected ProjProjectService projProjectService;

  @Override
  public SptSparepartInService getService() {
    return sptSparepartInService;
  }

  @Override
  public String getPageTitle() {
    return "备件管理";
  }

  @Override
  public String getProceessKey() {
    return "sparepartInProcess";
  }

  @RequestMapping("edit")
  @Override
  public ModelAndView edit(String id, HttpServletRequest req) throws Exception {
    ModelAndView mv = this.getModelAndView();

    SptSparepartIn model = null;

    AppUserService appUserService = ContextHolder
        .getSpringBean("appUserService");

    if (StringUtils.isEmpty(id)) {
      model = new SptSparepartIn();
      model.setId(HibernateUUIDGenerator.generate());
      model.setCreator(ContextHolder.getLoginUsername());

      model.setInstockDate(new Date());

    } else {
      model = this.getService().findOne(id);

    }

    mv.addObject("model", model);

    mv.addObject("creatorName",
        appUserService.findByUsername(model.getCreator()).getFullname());

    return mv;

  }

  @RequestMapping("inStoreEdit")
  public ModelAndView inStoreEdit(String id, HttpServletRequest req)
      throws Exception {
    ModelAndView mv = this.getModelAndView();
    SptSparepartIn model = null;
    AppUserService appUserService = ContextHolder
        .getSpringBean("appUserService");
    if (StringUtils.isNoneBlank(id)) {
      model = this.getService().findOne(id);
      String providerId = model.getProviderId();
      PrdProviderService prdProviderService = ContextHolder
          .getSpringBean("prdProviderService");
      PrdProvider ppd=prdProviderService.findOne(providerId);
      String ppdName=ppd.getName();
      mv.addObject("ppdName", ppdName);
    } else {
      model = new SptSparepartIn();
      model.setId(HibernateUUIDGenerator.generate());
      model.setCreator(ContextHolder.getLoginUsername());
      model.setInstockDate(new Date());
      mv.addObject("creatorName",
          appUserService.findByUsername(model.getCreator()).getFullname());

    }
    mv.addObject("model", model);
    /*
     * mv.addObject("projects", projProjectService.findAllPublished("id",
     * "name"));
     */
    return mv;

  }

  @RequestMapping("approveEdit")
  public ModelAndView approveEdit(String id, HttpServletRequest req)
      throws Exception {
    ModelAndView mv = this.getModelAndView();
    SptSparepartIn model = this.getService().findOne(id);
    mv.addObject("model", model);
    mv.addObject("projects", projProjectService.findAllPublished("id", "name"));
    return mv;

  }

  @RequestMapping("delete")
  @ResponseBody
  @Override
  public ResultMessage delete(String id, HttpServletRequest req) {

    BpmProcessRunService bpmProcessRunService = ContextHolder
        .getSpringBean("bpmProcessRunService");
    String process_Id = (String) this.getService()
        .findOneToMap(id, "process_Id").get("process_Id");

    if (StringUtils.isNotEmpty(process_Id))
      bpmProcessRunService.deleteProcessInstance(process_Id, "删除",
          ContextHolder.getLoginUsername());

    SptSparepartInDetailService sptSparepartInDetailService = ContextHolder
        .getSpringBean("sptSparepartInDetailService");

    sptSparepartInDetailService.deleteByInId(id);

    this.getService().delete(id);

    return ResultMessage.newInstance(true);
  }

  @RequestMapping("inSptlist")
  @ResponseBody
  public PageResponse<Map<String, Object>> inSptlist(String sparepartId,
      JqGridRequest request, HttpServletRequest req) {
    return sptSparepartInService
        .findAllSptDetailByRequest(sparepartId, request);
  }

  @RequestMapping("listSparepartCi")
  @ResponseBody
  public PageResponse<Map<String, Object>> listSparepartCi(String sparepartId,
      JqGridRequest request, HttpServletRequest req) {
    SptSparepartService sptSparepartService = ContextHolder
        .getSpringBean("sptSparepartService");
    request
        .setSearchString(" id in (select ci_id from spt_sparepart_relation where  sparepart_id='"
            + sparepartId + "')");
    return sptSparepartService.findAllDetailByRequest(request);
  }

}
